package com.kaizeli.website.controller;

import com.kaizeli.website.pojo.entity.AttendanceDO;
import com.kaizeli.website.pojo.vo.AttendanceVO;
import com.kaizeli.website.response.Result;
import com.kaizeli.website.service.AttendanceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * 考勤管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/attendance")
@RequiredArgsConstructor
@Tag(name = "考勤管理", description = "考勤记录管理相关接口")
@SecurityRequirement(name = "Bearer Authentication")
public class AttendanceController {
    
    private final AttendanceService attendanceService;
    
    @Operation(summary = "新增考勤记录", description = "创建新的考勤记录")
    @PostMapping
    public Result<Long> save(@RequestBody AttendanceDO attendanceDO) {
        log.info("新增考勤记录：{}", attendanceDO);
        Long result = attendanceService.save(attendanceDO);
        return Result.success(result);
    }
    
    @Operation(summary = "更新考勤记录", description = "根据ID更新考勤记录")
    @PutMapping("/{id}")
    public Result<Boolean> update(
            @Parameter(description = "考勤记录ID", required = true, example = "1")
            @PathVariable Long id,
            @RequestBody AttendanceDO attendanceDO) {
        log.info("更新考勤记录，ID：{}", id);
        attendanceDO.setId(id);
        Boolean result = attendanceService.update(attendanceDO);
        return Result.success(result);
    }
    
    @Operation(summary = "根据ID查询考勤记录", description = "根据ID查询考勤记录详情")
    @GetMapping("/{id}")
    public Result<AttendanceVO> getById(
            @Parameter(description = "考勤记录ID", required = true, example = "1")
            @PathVariable Long id) {
        log.info("根据ID查询考勤记录，ID：{}", id);
        AttendanceVO result = attendanceService.getById(id);
        return Result.success(result);
    }
    
    @Operation(summary = "根据ID删除考勤记录", description = "根据ID删除考勤记录")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteById(
            @Parameter(description = "考勤记录ID", required = true, example = "1")
            @PathVariable Long id) {
        log.info("根据ID删除考勤记录，ID：{}", id);
        Boolean result = attendanceService.deleteById(id);
        return Result.success(result);
    }
    
    @Operation(summary = "根据员工ID查询考勤记录", description = "根据员工ID查询考勤记录列表")
    @GetMapping("/employee/{employeeId}")
    public Result<List<AttendanceVO>> listByEmployee(
            @Parameter(description = "员工ID", required = true, example = "1")
            @PathVariable Long employeeId) {
        log.info("根据员工ID查询考勤记录，员工ID：{}", employeeId);
        List<AttendanceVO> result = attendanceService.listByEmployee(employeeId);
        return Result.success(result);
    }
    
    @Operation(summary = "根据项目ID查询考勤记录", description = "根据项目ID查询考勤记录列表")
    @GetMapping("/project/{projectId}")
    public Result<List<AttendanceVO>> listByProject(
            @Parameter(description = "项目ID", required = true, example = "1")
            @PathVariable Long projectId) {
        log.info("根据项目ID查询考勤记录，项目ID：{}", projectId);
        List<AttendanceVO> result = attendanceService.listByProject(projectId);
        return Result.success(result);
    }
    
    @Operation(summary = "根据员工ID和日期范围查询考勤记录", description = "根据员工ID和日期范围查询考勤记录列表")
    @GetMapping("/employee/{employeeId}/date-range")
    public Result<List<AttendanceVO>> listByEmployeeAndDateRange(
            @Parameter(description = "员工ID", required = true, example = "1")
            @PathVariable Long employeeId,
            @Parameter(description = "开始日期", required = true, example = "2025-01-01")
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @Parameter(description = "结束日期", required = true, example = "2025-01-31")
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {
        log.info("根据员工ID和日期范围查询考勤记录，员工ID：{}，开始日期：{}，结束日期：{}", employeeId, startDate, endDate);
        List<AttendanceVO> result = attendanceService.listByEmployeeAndDateRange(employeeId, startDate, endDate);
        return Result.success(result);
    }
    
    @Operation(summary = "根据项目ID和日期范围查询考勤记录", description = "根据项目ID和日期范围查询考勤记录列表")
    @GetMapping("/project/{projectId}/date-range")
    public Result<List<AttendanceVO>> listByProjectAndDateRange(
            @Parameter(description = "项目ID", required = true, example = "1")
            @PathVariable Long projectId,
            @Parameter(description = "开始日期", required = true, example = "2025-01-01")
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @Parameter(description = "结束日期", required = true, example = "2025-01-31")
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {
        log.info("根据项目ID和日期范围查询考勤记录，项目ID：{}，开始日期：{}，结束日期：{}", projectId, startDate, endDate);
        List<AttendanceVO> result = attendanceService.listByProjectAndDateRange(projectId, startDate, endDate);
        return Result.success(result);
    }
    
    @Operation(summary = "统计员工工作时长", description = "统计指定日期范围内员工的工作时长")
    @GetMapping("/employee/{employeeId}/work-hours")
    public Result<Double> sumWorkHours(
            @Parameter(description = "员工ID", required = true, example = "1")
            @PathVariable Long employeeId,
            @Parameter(description = "开始日期", required = true, example = "2025-01-01")
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @Parameter(description = "结束日期", required = true, example = "2025-01-31")
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {
        log.info("统计员工工作时长，员工ID：{}，开始日期：{}，结束日期：{}", employeeId, startDate, endDate);
        Double result = attendanceService.sumWorkHoursByEmployee(employeeId, startDate, endDate);
        return Result.success(result);
    }
}

